const { classModel, TeaAndClassModel, StuModel } = require("../model/index");
//创建班级
exports.createClass = async (req, res) => {
  const { className } = req.body;
  if (!className) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "参数不全",
    });
  }
  try {
    let result = await classModel.findOne({ className });
    if (result) {
      return res.status(400).json({
        code: 400,
        data: null,
        message: "班级名称已存在",
      });
    }

    let dbBack = new classModel({
      className
    });
    await dbBack.save();
    return res.status(200).json({
      code: 200,
      data: null,
      message: "创建成功",
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "创建失败",
    });
  }
};
//班级列表(等把教师做完就显示对应的教师名称)
exports.getClassList = async (req, res) => {
  const { pageNo = 1, pageSize = 5, key = "" } = req.query;
  if (key) {
    try {
      let result = await classModel
        .find({ className: { $regex: key } })
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .sort({ className: 1 });
      let count = await classModel.find({ className: { $regex: key } }).count();
      for(let i = 0 ; i < result.length ; i++) {
        let teacher = await TeaAndClassModel.findOne({classId:result[i]._id},'teaId').populate('teaId','teaName')
        result[i]._doc.teaID = teacher
      }
      return res.status(200).json({
        code: 200,
        data: {
          list: result,
          total: count,
        },
        message: "查询成功",
      });
    } catch (error) {
      return res.status(500).json({
        code: 500,
        data: null,
        message: "查询失败",
      });
    }
  } else {
    try {
      let result = await classModel
        .find()
        .skip((pageNo - 1) * pageSize)
        .limit(pageSize)
        .sort({ className: 1 })
      let count = await classModel.find().count();
      for(let i = 0 ; i < result.length ; i++) {
        let teacher = await TeaAndClassModel.findOne({classId:result[i]._id},'teaId').populate('teaId','teaName')
        result[i]._doc.teaID = teacher
      }
      return res.status(200).json({
        code: 200,
        data: {
          list: result,
          total: count,
        },
        message: "查询成功",
      });
    } catch (error) {
      return res.status(500).json({
        code: 500,
        data: null,
        message: "查询失败",
      });
    }
  }
};
//获取班级详情(等把教师做完就显示对应的教师名称)
exports.getClassDetail = async (req, res) => {
  const id = req.query.id;
  if (!id) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "班级id是必须的",
    });
  }
  try {
   let result = await classModel.findById(id)
   let studentList = await StuModel.find({className:result.className}).populate('jobId').sort({stuNum:1})
   return res.status(200).json({
    code:200,
    message:'获取成功',
    data:studentList
   })
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "查询失败",
    });
  }
};
//修改班级信息
exports.updateClass = async (req, res) => {
  const id = req.query.id;
  if (!id) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "班级id是必须的",
    });
  }
  try {
    await classModel.findByIdAndUpdate(id, req.body);
    return res.status(200).json({
      code: 200,
      data: null,
      message: "修改成功",
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "修改失败",
    });
  }
};
//删除班级
exports.deleteClass = async (req, res) => {
  const id = req.query.id;
  if (!id) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "班级id是必须的",
    });
  }
  try {
    let className = await classModel.findById(id)
    let student = await StuModel.find({className:className.className})
    if(student.length>0) {
      return res.status(400).json({
        code:400,
        data:null,
        message:'该班级下有学生，无法删除'
      })
    }
     await classModel.findByIdAndDelete(id);
     await TeaAndClassModel.deleteMany({classId:id})
    return res.status(200).json({
      code: 200,
      data: null,
      message: "删除成功",
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "删除失败",
    });
  }
};
